package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;

import com.project.mapper.IFacilitiesMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private IRoomMapper RMapper;
    @Autowired
    private IStudentMapper SMapper;
    @Autowired
    private IFacilitiesMapper FMapper;
    @Override
    public IPage<RoomBean> findByItem(Integer pageNo, String roomName, String roomState, String into, String roomSex) {
        return RMapper.findByItem(new Page(pageNo,5),roomName,roomState,into,roomSex);
    }

    @Override
    public RoomBean findById(Integer id) {
        RoomBean room = RMapper.selectById(id);
        room.setStudentList(SMapper.findByRoomId(id));
        return room;
    }

    @Override
    public boolean del(Integer id) {
        if (SMapper.countByRoom(id)==0){
            RMapper.deleteById(id);
            FMapper.deleteById(id);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public void add(RoomBean room) {
        RMapper.insert(room);
    }

    @Override
    public List<RoomBean> findBySex(String sex) {

        return RMapper.findBySex(sex);
    }
}
